function maxProfit2(prices: number[]): number {
    if (prices.length <= 1) return 0;
    // 到第i卖出获取到的最大收益
    // const dp: number[] = []

    // 状态转移方程
    // dp[i] = dp[i - 1] > prices[i] - minNumber ? dp[i - 1]  : prices[i] - minNumber

    // dp初始值为0
    // dp[0] = 0
    let preMaxProfit = 0

    // 历史最小值
    let min = prices[0]

    // 遍历更改状态
    for (let i = 1; i < prices.length; i++) {
        // 状态修改
        preMaxProfit = Math.max(prices[i] - min, preMaxProfit)
        // 取最小值
        min = Math.min(prices[i], min)
    }

    return preMaxProfit
}

console.log(maxProfit2([7,1,5,3,6,4]));
console.log(maxProfit2([7,6,4,3,1]));
console.log(maxProfit2([1,2,4]));
